home *** CD-ROM | disk | FTP | other *** search
/ Gold Medal Software 1 / Gold Medal Software Volume 1 (Gold Medal) (1994).iso / prog / tpwprog7.arj / CLIENT.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1992-07-02  |  7.9 KB  |  278 lines

  1. { client.pas -- Simple (cold link) DDE client }
  2.  
  3. program Client;
  4.  
  5. {$R client.res}
  6.  
  7. uses WinTypes, WinProcs, WObjects, Strings;
  8.  
  9. const
  10.  
  11.   id_Menu    = 100;    { Menu resource ID }
  12.   cm_Link    = 101;    { Link to server command ID }
  13.   cm_Unlink  = 102;    { Unlink from server command ID }
  14.   cm_GetData = 103;    { Get data command ID }
  15.   cm_Quit    = 104;    { Exit command ID }
  16.   id_Data    = 1;      { Static text control ID }
  17.  
  18.   defaultText = '(No text received)';  { Default data string }
  19.   msgCaption = 'Client Message Box';   { Message box caption }
  20.   wndCaption = 'Client';               { Window caption }
  21.   linkedCaption = 'Client-->Linked';   { Caption when linked }
  22.  
  23.   serverName = 'Server';  { Name of server application }
  24.   serverTopic = 'Data';   { Name of data to be transferred }
  25.  
  26. type
  27.  
  28.   ClientApplication = object(TApplication)
  29.     procedure InitMainWindow; virtual;
  30.   end;
  31.  
  32.   PConversation = ^TConversation;
  33.   TConversation = object(TStatic)
  34.     HWndServer: HWnd;          { Server's window handle }
  35.     LinkEstablished: Boolean;  { True if conversing with server }
  36.     constructor Init(AParent: PWindowsObject; AnId: Integer;
  37.       ATitle: PChar; X, Y, W, H: Integer; ATextLen: Word);
  38.     procedure WMDDEAck(var Msg: TMessage);
  39.       virtual wm_First + wm_DDE_Ack;
  40.     procedure WMDDEData(var Msg: TMessage);
  41.       virtual wm_First + wm_DDE_Data;
  42.     procedure WMDDETerminate(var Msg: TMessage);
  43.       virtual wm_First + wm_DDE_Terminate;
  44.     procedure LinkToServer;
  45.     procedure UnlinkFromServer;
  46.     procedure RequestText;
  47.   end;
  48.  
  49.   PClientWindow = ^ClientWindow;
  50.   ClientWindow = object(TWindow)
  51.     DDEWindow: PConversation;
  52.     constructor Init(AParent: PWindowsObject; ATitle: PChar);
  53.     procedure WMInitMenu(var Msg: TMessage);
  54.       virtual wm_First + wm_InitMenu;
  55.     procedure CMLink(var Msg: TMessage);
  56.       virtual cm_First + cm_Link;
  57.     procedure CMUnlink(var Msg: TMessage);
  58.       virtual cm_First + cm_Unlink;
  59.     procedure CMGetData(var Msg: TMessage);
  60.       virtual cm_First + cm_GetData;
  61.     procedure CMQuit(var Msg: TMessage);
  62.       virtual cm_First + cm_Quit;
  63.   end;
  64.  
  65.  
  66. { ClientApplication }
  67.  
  68. {- Initialize ClientApplication object's window }
  69. procedure ClientApplication.InitMainWindow;
  70. begin
  71.   MainWindow := New(PClientWindow, Init(nil, wndCaption))
  72. end;
  73.  
  74.  
  75. { ClientWindow }
  76.  
  77. {- Construct ClientWindow object }
  78. constructor ClientWindow.Init(AParent: PWindowsObject; ATitle: PChar);
  79. begin
  80.   TWindow.Init(AParent, ATitle);
  81.   with Attr do
  82.   begin
  83.     Menu := LoadMenu(HInstance, PChar(id_Menu));
  84.     X := 10; Y := 10; W := 275; H := 300
  85.   end;
  86.   DDEWindow := New(PConversation, Init(@Self, id_Data, defaultText,
  87.     10, 100, 200, 35, StrLen(defaultText)))
  88. end;
  89.  
  90. {- A menu is about to open. Gray any illegal commands. }
  91. procedure ClientWindow.WMInitMenu(var Msg: TMessage);
  92. begin
  93.   with Attr do
  94.   if Msg.WParam = Menu then
  95.   begin
  96.     if DDEWindow^.LinkEstablished then
  97.     begin
  98.       EnableMenuItem(Menu, cm_Link, mf_ByCommand or mf_Grayed);
  99.       EnableMenuItem(Menu, cm_Unlink, mf_ByCommand or mf_Enabled);
  100.       EnableMenuItem(Menu, cm_GetData, mf_ByCommand or mf_Enabled)
  101.     end else
  102.     begin
  103.       EnableMenuItem(Menu, cm_Link, mf_ByCommand or mf_Enabled);
  104.       EnableMenuItem(Menu, cm_Unlink, mf_ByCommand or mf_Grayed);
  105.       EnableMenuItem(Menu, cm_GetData, mf_ByCommand or mf_Grayed)
  106.     end
  107.   end
  108. end;
  109.  
  110. {- Establish a "conversation" with server }
  111. procedure ClientWindow.CMLink(var Msg: TMessage);
  112. begin
  113.   DDEWindow^.LinkToServer
  114. end;
  115.  
  116. {- Terminate the "conversation" with server }
  117. procedure ClientWindow.CMUnlink(var Msg: TMessage);
  118. begin
  119.   DDEWindow^.UnlinkFromServer
  120. end;
  121.  
  122. {- Request data from server }
  123. procedure ClientWindow.CMGetData(var Msg: TMessage);
  124. begin
  125.   DDEWindow^.RequestText
  126. end;
  127.  
  128. {- Exit application }
  129. procedure ClientWindow.CMQuit(var Msg: TMessage);
  130. begin
  131.   CloseWindow
  132. end;
  133.  
  134.  
  135. { TConversation }
  136.  
  137. {- Construct unlinked TConversation control window }
  138. constructor TConversation.Init(AParent: PWindowsObject; AnId: Integer;
  139.   ATitle: PChar; X, Y, W, H: Integer; ATextLen: Word);
  140. begin
  141.   TStatic.Init(AParent, AnId, ATitle, X, Y, W, H, ATextLen);
  142.   LinkEstablished := false
  143. end;
  144.  
  145. {- Respond to acknowledgement message from server }
  146. procedure TConversation.WMDDEAck(var Msg: TMessage);
  147. var
  148.   AppAtom, AppTopic: TAtom;
  149.   DDEStatus: Word;
  150. begin
  151.   AppTopic := HiWord(Msg.LParam);
  152.   if not LinkEstablished then
  153.   begin
  154.     HWndServer := Msg.WParam;  { Save server's window handle }
  155.     SetWindowText(Parent^.HWindow, linkedCaption);
  156.     LinkEstablished := true;
  157.     AppAtom := LoWord(Msg.LParam);
  158.     if AppAtom <> 0 then
  159.       GlobalDeleteAtom(AppAtom)
  160.   end else
  161.   begin
  162.     DDEStatus := LoWord(Msg.LParam);
  163.     if (DDEStatus and dde_Ack) = 0 then
  164.       MessageBox(Parent^.HWindow, 'Request for data failed',
  165.         msgCaption, mb_Ok)
  166.   end;
  167.   if AppTopic <> 0 then
  168.     GlobalDeleteAtom(AppTopic)
  169. end;
  170.  
  171. {- Respond to transmission of data from server }
  172. procedure TConversation.WMDDEData(var Msg: TMessage);
  173. var
  174.   AppTopic: TAtom;    { Global atom describing data topic }
  175.   HData: THandle;     { Handle to data's memory block }
  176.   PData: PDDEData;    { Pointer to data }
  177. begin
  178.   HData := LoWord(Msg.LParam);
  179.   AppTopic := HiWord(Msg.LParam);
  180.   {- Inspect topic here if necessary }
  181.   if AppTopic <> 0 then
  182.     GlobalDeleteAtom(AppTopic);
  183.   if HData <> 0 then
  184.   begin
  185.     PData := GlobalLock(HData);
  186.     if PData <> nil then
  187.     begin
  188.       if PData^.CfFormat = cf_Text then
  189.         SetText(PChar(@PData^.Value))
  190.       else
  191.         MessageBox(Parent^.HWindow, 'Data received is not text',
  192.           msgCaption, mb_Ok);
  193.       GlobalUnlock(HData)
  194.     end;
  195.     GlobalFree(HData) {- Assume client will dispose global data }
  196.   end
  197. end;
  198.  
  199. {- Respond to receipt of wm_DDE_Terminate message from server }
  200. procedure TConversation.WMDDETerminate(var Msg: TMessage);
  201. begin
  202.   if LinkEstablished then
  203.   begin
  204.     LinkEstablished := false;  { Cancel the conversation }
  205.     SetWindowText(Parent^.HWindow, wndCaption);
  206.     MessageBox(Parent^.HWindow, 'Conversation terminated by server',
  207.       msgCaption, mb_Ok)
  208.   end
  209. end;
  210.  
  211. {- Attempt to link TConversation window to server }
  212. procedure TConversation.LinkToServer;
  213. var
  214.   AppAtom, AppTopic: TAtom;
  215. begin
  216.   if not LinkEstablished then
  217.   begin
  218.     AppAtom := GlobalAddAtom(serverName);
  219.     AppTopic := GlobalAddAtom(serverTopic);
  220.     SendMessage(Word(-1), wm_DDE_Initiate, HWindow,
  221.       MakeLong(AppAtom, AppTopic));
  222.     GlobalDeleteAtom(AppAtom);
  223.     GlobalDeleteAtom(AppTopic)
  224.   end
  225. end;
  226.  
  227. {- Terminate link (if there is one) from server }
  228. procedure TConversation.UnlinkFromServer;
  229. begin
  230.   if LinkEstablished then
  231.   begin
  232.     if IsWindow(HWndServer) then
  233.       PostMessage(HWndServer, wm_DDE_Terminate, HWindow, 0)
  234.     else
  235.       MessageBox(Parent^.HWindow, 'Server lost', msgCaption, mb_Ok);
  236.     SetWindowText(Parent^.HWindow, wndCaption);
  237.     LinkEstablished := false
  238.   end
  239. end;
  240.  
  241. {- Request text data from server }
  242. procedure TConversation.RequestText;
  243. var
  244.   AppItem: TAtom;
  245. begin
  246.   if LinkEstablished then
  247.   begin
  248.     if IsWindow(HWndServer) then
  249.     begin
  250.       AppItem := GlobalAddAtom(serverTopic);
  251.       if not PostMessage(HWndServer, wm_DDE_Request, HWindow,
  252.         MakeLong(cf_Text, AppItem)) then
  253.         GlobalDeleteAtom(AppItem)
  254.     end else
  255.     begin
  256.       SetWindowText(Parent^.HWindow, wndCaption);
  257.       MessageBox(Parent^.HWindow, 'Server lost', msgCaption, mb_Ok);
  258.       LinkEstablished := false  { Lost server }
  259.     end
  260.   end
  261. end;
  262.  
  263. var
  264.  
  265.   ClientApp: ClientApplication;
  266.  
  267. begin
  268.   ClientApp.Init('ClientApp');
  269.   ClientApp.Run;
  270.   ClientApp.Done
  271. end.
  272.  
  273.  
  274. {--------------------------------------------------------------
  275.   Copyright (c) 1991 by Tom Swan. All rights reserved.
  276.   Revision 1.00    Date: 5/27/1991
  277. ---------------------------------------------------------------}
  278.